有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (1) 个答案

  1. # 1 楼答案

    获取收藏列表 每个数据库都有零个或多个集合。您可以从数据库中检索它们的列表(并打印出其中的任何内容):

    Set<String> colls = db.getCollectionNames();
    
    for (String s : colls) {
    System.out.println(s);
    }
    

    编辑:正如@Andrew的回答中所建议的,更新的java客户端使用以下内容:

    /**
     * Gets the names of all the collections in this database.
     *
     * @return an iterable containing all the names of all the collections in this database
     */
    MongoIterable<String> listCollectionNames();
    

    以及根据文档类型获取iterable集合:

    /**
     * Finds all the collections in this database.
     *
     * @param resultClass the class to decode each document into
     * @param <TResult>   the target document type of the iterable.
     * @return the list collections iterable interface
     * @mongodb.driver.manual reference/command/listCollections listCollections
     */
    <TResult> ListCollectionsIterable<TResult> listCollections(Class<TResult> resultClass);